
$(function(){
	$("#close_message").click(function(){
	   $("#object").fadeOut(1000);
	});
	
	$("#close_message1").click(function(){
	   $("#object1").fadeOut(1000);
	});
	
	
	$("#object").hide();
	$("#object1").hide();
	function hide()
	{
		
		$('#processbar').removeClass('error-image-process');
		$('#processbar').removeClass('loading-image-process');
		$('#processbar').removeClass('success-image-process');
		$("#object").removeClass('message_success');
		$("#object").removeClass('message_error');
		$("#object").removeClass('message_loading');
		$("#close_message").removeClass('close_message')
		$('#p_message').html("");
		
		$("#object1").removeClass('message_error1');
		$('#processbar1').removeClass('error-image-process1');
		$("#close_message1").removeClass('close_message1');
		$('#p_message1').html("");
		
		if($('#temp_keyword').val() == '')
		{
			$('#temp_keyword').val($('#title').val());
		} 
		
	
	}
	function disablebtn()
	{
		$('#scrap').attr('disabled','disabled');
		$('#keytag').attr('disabled','disabled');
		$('#sprinurl').attr('disabled','disabled');
		$('#gen_key').attr('disabled','disabled');
		$('#spinkey').attr('disabled','disabled');
		$('#gen_des').attr('disabled','disabled');
		$('#test_spin').attr('disabled','disabled');
	}
	function enablebtn()
	{
		$('#scrap').removeAttr('disabled');
		$('#keytag').removeAttr('disabled');
		$('#sprinurl').removeAttr('disabled');
		$('#gen_key').attr('disabled','disabled');
		$('#spinkey').attr('disabled','disabled');
		$('#gen_des').attr('disabled','disabled');
		$('#test_spin').attr('disabled','disabled');
	}
	function m_loading(msg)
	{
		
		$('#p_message').html(msg);
		
		$('#processbar').addClass('loading-image-process');
		$("#close_message").addClass('close_message');
		$("#object").addClass('message_loading').animate( 20 ).fadeIn(3000);
		disablebtn();
	}
	function m_success(msg)
	{
		
		$('#p_message').html(msg);
		
		$('#processbar').addClass('success-image-process');
		$("#close_message").addClass('close_message');
		$("#object").addClass('message_success').animate( 20 ).fadeIn(3000);
		enablebtn();
		
	}
	function m_error(msg)
	{
		
		$('#p_message').html(msg);
		
		$('#processbar').addClass('error-image-process');
		$("#close_message").addClass('close_message');
		$("#object").addClass('message_error').animate( 20 ).fadeIn(3000);
		enablebtn();
		
	}
	function m_error1(msg)
	{
		
		$('#p_message1').html(msg);
		
		$('#processbar1').addClass('error-image-process');
		$("#close_message1").addClass('close_message');
		$("#object1").addClass('message_error').animate( 20 ).fadeIn(3000);
		enablebtn();
		
	}
	
	
   
   //close the message box when the cross image is clicked 
  
	$("#scrap").click(function(){	
		var url = $('#url-0').val();
		var urlget = $('#scrap').attr('urlget');
	    hide();
		if(url == '' || url =='http://')
		{
					alert('Please input your URL');				
		}
		else
		{	
			$.ajax({
					url: urlget,
					type: "GET",
					data: 'url=' + url,
					dataType: "html",
					beforeSend: function(){	
						m_loading("Scrapping and spin meta data tags " + url + " ...");			
					},
					success: function(html){	
						obj = JSON.parse(html);
						if(obj.error != "")
						{
						
							m_error1(obj.error);
							m_success("Scrap and spin meta data tags from " + url +" completed");
						}
						else
						{
							m_success("Scrap and spin meta data tags from " + url +" completed");
							$('#title-0').val(obj.keywords);
							
							$('#description').val(obj.description);
							$('#tags-0').val(obj.tags);	
							if(obj.error_lenght != "")
							{
								m_error1(obj.error_lenght);
							} 
						}

					}	
				});	
		}
	});
	$("#keytag").click(function(){	
		var url = $('#url-0').val();
		var urlget = $('#keytag').attr('urlget');
	    hide();
		if(url == '' || url =='http://')
		{
					alert('Please input your URL');				
		}
		else
		{	
			$.ajax({
					url: urlget,
					type: "GET",
					data: 'url=' + url,
					dataType: "html",
					beforeSend: function(){	
						m_loading("Scrapping and spin meta data tags from " + url + " ...");
									
					},
					success: function(html){	
						obj = JSON.parse(html);
						if(obj.error != "")
						{
							
							m_error1(obj.error);
							m_success("Scrap and spin meta data tags from " + url + " completed");
						}
						else
						{
							m_success("Scrap and spin meta data tags from " + url + " completed");
							$('#title-0').val(obj.keywords);
							
							$('#description').val(obj.description);
							$('#tags-0').val(obj.tags);	
							if(obj.error_lenght != "")
							{
								m_error1(obj.error_lenght);
							} 
						}

					}	
				});	
		}
	});
	
	$("#spinurl").click(function(){	
		var urls = $('#url-0').val();
		var urlget = $('#spinurl').attr('urlget');
	     hide();
		if(urls == '' || $('#url-0').val().indexOf(",")==-1)
		{
					alert('Please input your URLs separated by commas');				
		}
		else
		{	
			$.ajax({
					url: urlget,
					type:"GET",
					data: 'url=' + urls,
					dataType: "html",
					beforeSend: function(){	
						m_loading("Spinning URLs field : \"" + urls + "\" from separated by commas to spin format...");
									
					},
					success: function(html){	
						obj = JSON.parse(html);
						if(obj.error != "")
						{
							
							m_error1(obj.error);
						}
						else
						{
							m_success("Spinning URLs field : \"" + urls + "\" from separated by commas to spin format comleted");
							$('#url-0').val(obj.urls);
							$('#title-0').val(obj.keywords);
							
							$('#description').val(obj.description);
							$('#tags-0').val(obj.tags);	
						}

					}	
				});	
		}
	});
	
	//gen_key
	$("#gen_key").click(function(){	
		var urlget = $('#gen_key').attr('urlget');
		 hide();
		if ($('#title-0').val().indexOf("|")==-1 || $('#title-0').val().indexOf("{")==-1 || $('#title-0').val().indexOf("}")==-1)
		{
			var keyword = $('#title-0').val();
		}
		else if($('#title-0').val() == '' || $('#title-0').val().indexOf("|")!=-1 || $('#title-0').val().indexOf("{")!=-1 || $('#title-0').val().indexOf("}")!=-1)
		{
			var keyword = '';
		}
		else
		{
			var keyword = $('#temp_keyword').val();
		}
		var mySplitResult = keyword.split(",");
		while (mySplitResult.length > 3 || keyword == '')
		{
			if(keyword=='')
			{
				keyword=window.prompt("Please enter some keywords you want to generate Longtail Keywords from Google Suggestion (MAX 3) - separated by commas","");
			}
			else
			{
				if(mySplitResult.length>3)
				{
					keyword=window.prompt("Please only use 3 keywords you want to generate Longtail Keywords from Google Suggestion (MAX 3 - Currently Keyword(s) : "+ mySplitResult.length+") - separated by commas",keyword);
				}
			}
				mySplitResult = keyword.split(",");
		}			
			

		if(keyword == null)
		{
			exit();	
		}
		var char = $('#character').val();
		$.ajax({
					url: urlget,
					type: "GET",
					data: 'char='+char+'&keyword='+keyword,
					dataType: "html",
					beforeSend: function(){
						
						m_loading("Scrapping and spin longtail keywords for keyword \" "+keyword+"\" from Google Suggestion...");
					},
					success: function(html){
						obj = JSON.parse(html);
						if(obj.error != "")
						{
							
							m_error1(obj.error);
							
							
						}
						else
						{
							$('#title-0').val(obj.keyword);						
							m_success("Scrap and spin longtail keywords for keyword \" "+keyword+"\" completed - "+obj.total+" Keywords generated");

						}
					}
					
		});
	});
	
	//spin key
	$("#spinkey").click(function(){	
		var keyword = $('#title-0').val();
		var urlget = $('#spinkey').attr('urlget');
	     hide();
		if(keyword == '' || $('#title-0').val().indexOf(",")==-1)
		{
				alert('Please input your Keywords separated by commas');	
		}
		else
		{	
			$.ajax({
					url: urlget,
					type:"GET",
					data: 'keyword=' + keyword,
					dataType: "html",
					beforeSend: function(){	
						m_loading("Spinning Keywords field : \""+keyword+"\" from separated by commas to spin format...");
									
					},
					success: function(html){	
						obj = JSON.parse(html);
						if(obj.error != "")
						{
							
							m_error1(obj.error);
						}
						else
						{
							m_success("Spinning Keywords field : \""+keyword+"\" from separated by commas to spin format comleted");
							$('#temp_keyword').val(keyword);
							
							$('#title-0').val(obj.keywords);
							
							$('#description').val(obj.description);
							$('#tags-0').val(keyword);	
						}

					}	
				});	
		}
	});
	//gend des
	$("#gen_des").click(function(){	
		var urlget = $('#gen_des').attr('urlget');
		var keyword = $('#temp_keyword').val();
		 hide();
		if ($('#title-0').val().indexOf(",")!=-1)
		{
				keyword = $('#title-0').val();
		}
		var mySplitResult = keyword.split(",");
		while (mySplitResult.length > 3 || keyword == '')
		{
			if(keyword=='')
			{
				keyword=window.prompt("Please enter some keywords you want to generate description from Article Directory (MAX 5) - separated by commas","");
			}
			else
			{
				if(mySplitResult.length>5)
				{
					keyword=window.prompt("Please only use 5 keywords you want to generate description from Article Directory (MAX 5 - Currently Keyword(s) : "+ mySplitResult.length+") - separated by commas",keyword);
				}
			}
			mySplitResult = keyword.split(",");
		}			
			
		if(keyword == null)
		{
			exit();	
		}
	
		var num = $('#des_num').val();
		$.ajax({
					url: urlget,
					type: "GET",
					data: 'num='+num+'&keyword='+keyword,
					dataType: "html",
					beforeSend: function(){
						
						m_loading("Scrapping and spin Description for keyword \" "+keyword+"\" from Article Directory...");
					},
					success: function(html){
						obj = JSON.parse(html);
						if(obj.error != "")
						{
							
							m_error1(obj.error);
							
						}
						else
						{
							$('#description').val(obj.des);
							$('#tags-0').val(obj.tags);
							m_success("Scrap and spin Description for keyword \" "+keyword+"\" completed - "+obj.total+" Description generated");

						}
					}
					
		});
	});
	
	//test spin
	$("#test_spin").click(function(){	
		var text = $('#description').val();
		var urlget = $('#test_spin').attr('urlget');
	     hide();
		if(text == '')
		{
			alert('Please input your description in spin format for testing');	
		}
		else
		{	
			$.ajax({
					url: urlget,
					type:"GET",
					data: 'text=' + text,
					dataType: "html",
					beforeSend: function(){	
						m_loading("Spinning Description ...");
									
					},
					success: function(html){		
							m_success("Example Description : <br><br>"+html);
					}	
				});	
		}
	});
	
	
	
});	